package com.ljc.cjgl.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.entitiy.Course;
import com.ljc.cjgl.entitiy.DbFile;
import com.ljc.cjgl.form.FileForm;
import com.ljc.cjgl.repository.FileRepository;

@Service
public class FileService {
	
	@Autowired
	private FileRepository fileDAO;
	public DbFile findById(Integer id) {
		Optional<DbFile> optional = fileDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	
	public List<DbFile> findAll(){
		return fileDAO.findAll();
	}
	
	public Page<DbFile> findAll(Specification<DbFile> specification,Pageable pageable){
		return fileDAO.findAll(specification, pageable);
	}
	
	@Transactional
	public synchronized boolean save(MultipartFile file, FileForm form,String name) {
		String filePath = "d:/server_file";
		File fd=new File(filePath);
		if(!fd.exists()) {
			fd.mkdir();
		}
		try {
	    	DbFile model=new DbFile();
	    	if(form.getId()!=null) {
	    		model=findById(form.getId());
	    	}else {
	    		model.setCreator(name);
	    		model.setDate(new Date());
	    	}
	    	Integer courseId = form.getCourseId();
	    	if(courseId!=null) {
	    		model.setCourse(new Course(courseId));
	    	}
	    	BeanUtils.copyProperties(form, model,"id","filename");
	    	String filename = file.getOriginalFilename();
	    	if(StringUtils.hasText(filename)) {
		    	filename=(new File(filename)).getName();
		    	model.setFilename(filename);
		    	String uid=model.getUid();
		    	if(uid==null) {
		    		uid=UUID.randomUUID().toString();
		    		model.setUid(uid);
		    	}
//		    	FileUtil.uploadFile(file.getBytes(), uid);
		    	File targetFile=new File(filePath, uid);
		    	file.transferTo(targetFile);
	    	}
			fileDAO.save(model);
			return true;
		} catch (IOException e) {
			return false;
		}
	}
	
	
	public synchronized boolean delete(List<DbFile> files) {
		try {
			for (DbFile file : files) {
				String uid = file.getUid();
				File f=new File(FileUtil.getFilePath(),uid);
				if(f.exists()) {
					f.delete();
				}
				fileDAO.delete(file);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public DbFile findByUid(String uid) {
		return fileDAO.findByUid(uid);
	}

	public Page<DbFile> findAll(PageRequest pageRequest) {
		return fileDAO.findAll(pageRequest);
	}

	public List<Course> findCourse() {
		return fileDAO.findDistinctCourse();
	}

	public List<DbFile> findByCourseId(Integer id) {
		return fileDAO.findByCourseId(id);
	}
	
	
	
	
}
